﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.StackTraceUtility
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Diagnostics;
using System.Reflection;
using System.Security;
using System.Text;
using UnityEngine.Scripting;

#nullable disable
namespace UnityEngine
{
  public static class StackTraceUtility
  {
    private static string projectFolder = "";

    [RequiredByNativeCode]
    internal static void SetProjectFolder(string folder)
    {
      StackTraceUtility.projectFolder = folder;
      if (string.IsNullOrEmpty(StackTraceUtility.projectFolder))
        return;
      StackTraceUtility.projectFolder = StackTraceUtility.projectFolder.Replace("\\", "/");
    }

    [RequiredByNativeCode]
    [SecuritySafeCritical]
    public static unsafe string ExtractStackTrace()
    {
      int bufferMax = 16384;
      byte* buffer = stackalloc byte[bufferMax];
      int stackTraceNoAlloc = Debug.ExtractStackTraceNoAlloc(buffer, bufferMax, StackTraceUtility.projectFolder);
      return stackTraceNoAlloc > 0 ? new string((sbyte*) buffer, 0, stackTraceNoAlloc, Encoding.UTF8) : StackTraceUtility.ExtractFormattedStackTrace(new StackTrace(1, true));
    }

    public static string ExtractStringFromException(object exception)
    {
      string message;
      string stackTrace;
      StackTraceUtility.ExtractStringFromExceptionInternal(exception, out message, out stackTrace);
      return message + "\n" + stackTrace;
    }

    [SecuritySafeCritical]
    [RequiredByNativeCode]
    internal static void ExtractStringFromExceptionInternal(
      object exceptiono,
      out string message,
      out string stackTrace)
    {
      if (exceptiono == null)
        throw new ArgumentException("ExtractStringFromExceptionInternal called with null exception");
      if (!(exceptiono is Exception exception))
        throw new ArgumentException("ExtractStringFromExceptionInternal called with an exceptoin that was not of type System.Exception");
      StringBuilder stringBuilder = new StringBuilder(exception.StackTrace == null ? 512 : exception.StackTrace.Length * 2);
      message = "";
      string str1 = "";
      for (; exception != null; exception = exception.InnerException)
      {
        str1 = str1.Length != 0 ? exception.StackTrace + "\n" + str1 : exception.StackTrace;
        string str2 = exception.GetType().Name;
        string str3 = "";
        if (exception.Message != null)
          str3 = exception.Message;
        if (str3.Trim().Length != 0)
          str2 = str2 + ": " + str3;
        message = str2;
        if (exception.InnerException != null)
          str1 = "Rethrow as " + str2 + "\n" + str1;
      }
      stringBuilder.Append(str1 + "\n");
      StackTrace stackTrace1 = new StackTrace(1, true);
      stringBuilder.Append(StackTraceUtility.ExtractFormattedStackTrace(stackTrace1));
      stackTrace = stringBuilder.ToString();
    }

    [SecuritySafeCritical]
    internal static string ExtractFormattedStackTrace(StackTrace stackTrace)
    {
      StringBuilder stringBuilder = new StringBuilder((int) byte.MaxValue);
      for (int index1 = 0; index1 < stackTrace.FrameCount; ++index1)
      {
        StackFrame frame = stackTrace.GetFrame(index1);
        MethodBase method = frame.GetMethod();
        if (method != null)
        {
          System.Type declaringType = method.DeclaringType;
          if (declaringType != null)
          {
            string str1 = declaringType.Namespace;
            if (!string.IsNullOrEmpty(str1))
            {
              stringBuilder.Append(str1);
              stringBuilder.Append(".");
            }
            stringBuilder.Append(declaringType.Name);
            stringBuilder.Append(":");
            stringBuilder.Append(method.Name);
            stringBuilder.Append("(");
            int index2 = 0;
            ParameterInfo[] parameters = method.GetParameters();
            bool flag = true;
            for (; index2 < parameters.Length; ++index2)
            {
              if (!flag)
                stringBuilder.Append(", ");
              else
                flag = false;
              stringBuilder.Append(parameters[index2].ParameterType.Name);
            }
            stringBuilder.Append(")");
            string str2 = frame.GetFileName();
            if (str2 != null && (!(declaringType.Name == "Debug") || !(declaringType.Namespace == "UnityEngine")) && (!(declaringType.Name == "Logger") || !(declaringType.Namespace == "UnityEngine")) && (!(declaringType.Name == "DebugLogHandler") || !(declaringType.Namespace == "UnityEngine")) && (!(declaringType.Name == "Assert") || !(declaringType.Namespace == "UnityEngine.Assertions")) && (!(method.Name == "print") || !(declaringType.Name == "MonoBehaviour") || !(declaringType.Namespace == "UnityEngine")))
            {
              stringBuilder.Append(" (at ");
              if (!string.IsNullOrEmpty(StackTraceUtility.projectFolder) && str2.Replace("\\", "/").StartsWith(StackTraceUtility.projectFolder))
                str2 = str2.Substring(StackTraceUtility.projectFolder.Length, str2.Length - StackTraceUtility.projectFolder.Length);
              stringBuilder.Append(str2);
              stringBuilder.Append(":");
              stringBuilder.Append(frame.GetFileLineNumber().ToString());
              stringBuilder.Append(")");
            }
            stringBuilder.Append("\n");
          }
        }
      }
      return stringBuilder.ToString();
    }
  }
}
